from PyInstaller.utils.hooks import collect_all
import os

# 获取 PaddleOCR 模型路径
def get_paddleocr_models():
    import paddleocr
    model_path = os.path.join(paddleocr.__path__[0], "whl")
    if os.path.exists(model_path):
        return [(model_path, "paddleocr/whl")]
    return []

datas = []
binaries = []
hiddenimports = []

# 收集 PaddleOCR 相关依赖
tmp_ret = collect_all('paddleocr')
datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]

# 收集其他关键依赖
for lib in ['paddle', 'numpy', 'opencv-python', 'pyclipper', 'shapely', 'scipy', 'lmdb', 'skimage', 'paddlex',]:
    tmp_ret = collect_all(lib)
    datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]

# 添加 PaddleOCR 模型文件
datas += get_paddleocr_models()
datas += (r'F:\work\pythonProject1\venv\Lib\site-packages\setuptools\_distutils', 'setuptools/_distutils'),
a = Analysis(
    ['main.py'],
     pathex=['F:\\work\\pythonProject1\\venv\\Lib\\site-packages',
    'F:\\work\\pythonProject1\\venv\\Lib\\site-packages\\paddle\\libs'],
    binaries=binaries+[('F:\\work\\pythonProject1\\venv\\Lib\\site-packages\\paddle\\libs','.')],
    datas=datas,
    hiddenimports=hiddenimports+['importlib_resources.trees','mysqlclient'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='ocrapp',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='ocrapp',
)
